#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_zero = 1,
    Mod_zero,
    Unknow_oper
};

class Calculate
{
public:
    Calculate()
    {
    }

    void Execute(Request request, Response *response)
    {
        char oper = request.op;
        switch (oper)
        {
        case '+':
            response->_result = request.x + request.y;
            response->_code = 0;
            break;
        case '-':
            response->_result = request.x - request.y;
            response->_code = 0;
            break;
        case '*':
            response->_result = request.x * request.y;
            response->_code = 0;
            break;
        case '/':
            if (0 == request.y)
            {
                response->_code = Div_zero;
            }
            else
            {
                response->_result = request.x / request.y;
                response->_code = 0;
            }
            break;
        case '%':
            if (0 == request.y)
            {
                response->_code = Mod_zero;
            }
            else
            {
                response->_result = request.x % request.y;
                response->_code = 0;
            }
            break;
        default:
            response->_code = Unknow_oper;
            break;
        }
    }

    std::string Compute(std::string &package)
    {
        std::string content;
        bool d = Decode(package, &content);
        if (!d)
            return "";

        Request reqs;
        Response resp;

        bool r = reqs.Deserialization(content);
        if (!r)
            return "";
        Execute(reqs, &resp);

        content = "";
        resp.Serialization(&content);
        std::string ret_pack = Encode(content);

        return ret_pack;
    }

    ~Calculate()
    {
    }
};